package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.BaseContext;
import com.itheima.youdianda.entity.YddArticle;
import com.itheima.youdianda.entity.YddUser;
import com.itheima.youdianda.entity.YddUserData;
import com.itheima.youdianda.mapper.YddUserMapper;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddUserDataService;
import com.itheima.youdianda.service.YddUserService;
import com.itheima.youdianda.util.MyPages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Phaser;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {

	@Autowired
	private YddUserDataService yddUserDataService;

	@Autowired
	private YddArticleService yddArticleService;

	@Override
	public Map<String, Object> getUserInfo() {
		// 获取当前登录用户id
		Long currentId = BaseContext.getCurrentId();

		// userInfo
		YddUser userInfo = this.getById(currentId);
		// userInfo

		// articleNum
		LambdaQueryWrapper<YddArticle> articleWrapper = new LambdaQueryWrapper<>();
		articleWrapper.eq(YddArticle::getUser_id,currentId);
		int articleNum = yddArticleService.count(articleWrapper);
		// articleNum

		// collectNum
		// 根据 user_id去查 ydd_user_data 表,查询关联的article
		LambdaQueryWrapper<YddUserData> userDataWrapper = new LambdaQueryWrapper<>();
		userDataWrapper.eq(YddUserData::getUser_id,currentId).eq(YddUserData::getType,1);
		int collectNum = yddUserDataService.count(userDataWrapper);
		// collectNum

		// zanNum
		// 根据 user_id去查 ydd_user_data 表,查询关联的article
		LambdaQueryWrapper<YddUserData> userDataWrapper1 = new LambdaQueryWrapper<>();
		userDataWrapper1.eq(YddUserData::getUser_id,currentId).eq(YddUserData::getType,2);
		int zanNum = yddUserDataService.count(userDataWrapper1);
		// zanNum

		Map<String,Object> map = new HashMap<>();
		map.put("userInfo",userInfo);
		map.put("articleNum",articleNum);
		map.put("collectNum",collectNum);
		map.put("zanNum",zanNum);
		return map;
	}

	@Override
	public Map<String, Object> userDataList(String page, String limit, String type) {
		Map<String, Object> map = new HashMap<>();
		MyPages<YddArticle> list = new MyPages<>();
		// 获取当前用户id
		Long currentId = BaseContext.getCurrentId();
		LambdaQueryWrapper<YddUserData> yddUserDataWrapper = new LambdaQueryWrapper<>();
		yddUserDataWrapper.eq(YddUserData::getUser_id,currentId)
				.eq(YddUserData::getType,type);
		List<YddUserData> userDataList = yddUserDataService.list(yddUserDataWrapper);
		List<Integer> articleIds = new ArrayList<>();
		for (YddUserData yddUserData : userDataList) {
			articleIds.add(yddUserData.getArticle_id());
		}

		Page<YddArticle> yddArticlePage = new Page<>(Long.parseLong(page),Long.parseLong(limit));
		LambdaQueryWrapper<YddArticle> yddArticleWrapper = new LambdaQueryWrapper<>();
		yddArticleWrapper.in(YddArticle::getId,articleIds);
		yddArticleService.page(yddArticlePage,yddArticleWrapper);

		List<YddArticle> myRecords = yddArticlePage.getRecords();
		list.setCount(yddArticlePage.getTotal());
		list.setTotalPages(yddArticlePage.getPages());
		list.setPageSize(yddArticlePage.getSize());
		list.setCurrentPage(yddArticlePage.getCurrent());
		list.setData(myRecords);

		map.put("list",list);
		return map;
	}
}
